from .spider import Spider
from bs4 import BeautifulSoup
from urllib import parse


class LuoQiuSpider(Spider):
    def __init__(self):
        Spider.__init__(self)

    def resolve_catalog(self, url):
        res = self.request(url)
        soup = BeautifulSoup(res,'html.parser')
        fiction_list = soup.select('#defaulthtml4 td a')

        fiction_catalog_list = []

        for fiction_el in fiction_list:
            href = fiction_el.attrs['href']
            href_url = parse.urlparse(href)

            path = href_url.path[href_url.path.rfind('/'):-1]

            fiction = {'url': fiction_el.attrs['href'], 'text': fiction_el.string,'code' : path}

            fiction_catalog_list.append(fiction)

        return  fiction_catalog_list
